package pe.com.cineplanet.dao.impl;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import pe.com.cineplanet.bean.Company;
import pe.com.cineplanet.bean.Politic;
import pe.com.cineplanet.bean.Role;
import pe.com.cineplanet.bean.User;
import pe.com.cineplanet.dao.UserDAO;

public class UserImpl implements UserDAO {

	
	protected JdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public User select(String usuario, String password) {
		StringBuffer strQuery= new StringBuffer();
		strQuery.append(" SELECT u.id, u.names, u.lastnames, u.email, c.id as companyId, c.name as companyName, r.id as roleId, r.name as roleName, c.locale, u.active ");
		strQuery.append(" FROM sec_user u inner join sec_company c on u.id_com = c.id ");
		strQuery.append(" inner join sec_role r on u.id_role=r.id and u.id_com=r.id_com ");
		strQuery.append(" where u.username=? and u.password=?");
	
		Object[] params = new Object[]{
				usuario, password
		};
		
		try {
			return (User)jdbcTemplate.queryForObject(strQuery.toString(), params, new RowMapper() {
				
				public User mapRow(ResultSet rs, int arg1) throws SQLException {
					User user = new User(
							rs.getInt("id"),
							rs.getString("names"),
							rs.getString("lastnames"),
							new Role(rs.getInt("roleId"), 
									rs.getString("roleName"),
									new Company(
									rs.getInt("companyId"), 
									rs.getString("companyName"), 
									rs.getString("locale"))
							),
							rs.getString("email"),
							rs.getBoolean("active")
						);
					
					return user;
				}
			});
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
		
		

	}
	

	@SuppressWarnings("unchecked")
	public List<Politic> getPolitics(int idprofile) {
		StringBuffer strQuery= new StringBuffer();
		strQuery.append(" SELECT p.id, p.description, p.type, p.parent, p.module ");
		strQuery.append(" FROM sec_politic p inner join sec_politic_role_company pc on p.id = pc.id_pol ");
		strQuery.append(" inner join sec_company c on c.id = pc.id_com ");
		strQuery.append(" inner join sec_role r on r.id = pc.id_role ");
		strQuery.append(" inner join sec_user u on u.id_com=c.id and u.id_role=r.id ");
		strQuery.append(" WHERE r.id=? ");
	
		Object[] params = new Object[]{
				idprofile
		};
		

		return jdbcTemplate.query(strQuery.toString(), params, new RowMapper() {
			
			public Politic mapRow(ResultSet rs, int arg1) throws SQLException {
				Politic politic = new Politic(
						rs.getInt("id"),
						rs.getString("description"),
						rs.getInt("type"),
						rs.getInt("parent"),
						rs.getInt("module")
						);
				
				return politic;
			}
		});
		

	}
	   
	/*
	public Integer insert(final String log, final String AuditoriaType, final String tipo)
	{
		
		return (Integer)jdbcTemplate.execute(new CallableStatementCreator() {
			
			public CallableStatement createCallableStatement(Connection c)
					throws SQLException {
				CallableStatement cs = c.prepareCall("{call spInsertarAuditoriaLog(?,?,?,?,?)}");
				cs.registerOutParameter(1, Types.INTEGER);
				
				cs.setInt(1, 0);
				cs.setString(2, AuditoriaType);
				cs.setString(3, tipo);
				cs.setString(4, "swaohuser");
				cs.setString(5, log);
				
				return cs;
			}
			
		}, new CallableStatementCallback() {
			

			public Integer doInCallableStatement(CallableStatement cs)
					throws SQLException, DataAccessException {
				cs.execute();
				return cs.getInt("resultado");
			}
			
		});
		
	}
	*/
	
}
